package com.ytjj.common.utils.feishu;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.ytjj.common.utils.HttpClientUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 飞书异常监控
 */

@Slf4j
@Service
public class FeiShuUtil {

    @Getter
    public static String URL;

    @Getter
    public static String URLMXL;

    @Getter
    public static String YXURL;


    @Getter
    public static String DZURL;

    /**
     * 平台运维机器人
     */
    @Getter
    public static String YWURL;

    /**
     * 订单数据预警机器人
     */
    @Getter
    public static String ORDERURL;

    /**
     * 退款预警机器人
     */
    @Getter
    public static String REFUNDURL;

    /**
     * 微信投诉预警机器人
     */
    @Getter
    public static String COMPLAINTSURL;



    @Value("${feiShuUrl:}")
    public void setUrl(String feiShuUrl) {
        URL = feiShuUrl;
    }

    @Value("${mxlfeiShuUrl:}")
    public void setUrlMXL(String mxlfeiShuUrl) {
        URLMXL = mxlfeiShuUrl;
    }

    @Value("${yxfeiShuUrl:}")
    public void setYxUrl(String yxfeiShuUrl) {
        YXURL = yxfeiShuUrl;
    }

    @Value("${dzfeiShuUrl:}")
    public  void setDZURL(String dzurl) {
        DZURL = dzurl;
    }


    @Value("${ywfeiShuUrl:}")
    public  void setYWURL(String ywurl) {
        YWURL = ywurl;
    }

    @Value("${orderFeiShuUrl:}")
    public  void setORDERURL(String orderUrl) {
        ORDERURL = orderUrl;
    }

    @Value("${refundFeiShuUrl:}")
    public  void setREFUNDURL(String refundUrl) {
        REFUNDURL = refundUrl;
    }

    @Value("${complaintFeiShuUrl:}")
    public  void setComplaintsURL(String complaintsUrl) {
        COMPLAINTSURL = complaintsUrl;
    }

    /**
     * 发送报警消息
     * @param msg
     */
    public static void sendMsg(String msg){
        sendMsg(msg, URL);
    }

    public static void sendMsgMxl(String msg){
        sendMsg(msg, URLMXL);
    }

    /**
     * 发送报警消息(运营招商相关报警发这个)
     * @param msg
     */
    public static void sendYxMsg(String msg){
        sendMsg(msg, YXURL);
    }

    /**
     * 地址异常报警
     * @param msg
     */
    public static void sendDzMsg(String msg){
        sendMsg(msg, DZURL);
    }

    /**
     * 运维相关报警
     * @param msg
     */
    public static void sendYwMsg(String msg){
        sendMsg(msg, YWURL);
    }

    /**
     * 下单数量预警
     * @param msg
     */
    public static void sendOrderNumMsg(String msg){
        sendMsg(msg, ORDERURL);
    }

    /**
     * 退款余额不足预警
     * @param msg
     */
    public static void sendRefundMsg(String msg){
        sendMsg(msg, REFUNDURL);
    }

    /**
     * 用户微信投诉预警
     * @param msg
     */
    public static void sendComplaintsMsg(String msg){
        sendMsg(msg, COMPLAINTSURL);
    }

    private static void sendMsg(String msg, String complaintsurl) {
        try {
            if (StringUtils.isBlank(msg) || StringUtils.isBlank(complaintsurl)){
                return;
            }
            Map<String,Object> map = Maps.newHashMap();
            map.put("msg_type","text");
            map.put("content",new FeishuEntity(msg));
            String jsonStr = JSON.toJSONString(map);
            HttpClientUtil.doPostJson(complaintsurl,jsonStr);
        } catch (Exception e) {
            log.error("=================发送飞书异常消息出错,{}",e.getMessage());
        }
    }


    public static void main(String[] args) {
        sendMsgMxl("测试报警,不用管");
    }
}
